﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using LinQProject;
using ServiceProject.EntityService;

public partial class controls_ucMenuProducts : System.Web.UI.UserControl
{
    protected LinQProject.FarmacieDataClassesDataContext database = new LinQProject.FarmacieDataClassesDataContext();
    protected CategoryService categService;
    //protected ProductCategoryService prodCategService;

    public controls_ucMenuProducts()
    {
        //prodCategService = new ProductCategoryService(database);
        categService = new CategoryService(database);
    }

    public int CategoryId
    {
        get
        {
            int intValue;
            if (Int32.TryParse(hdnCategoryId.Value, out intValue))
            {
                return intValue;
            }
            return -1;
        }
        set { hdnCategoryId.Value = value.ToString(); }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        BindProducts();
        
    }

    private void BindProducts()
    {
        if (CategoryId <0)
            return;
        Category categ = categService.GetActiveCategoryById(CategoryId);
        if (categ == null)
            return;
        lblCategory.Text = categ.categoryName;
        rptProducCategory.DataSource = categService.GetAllActiveCategory();
        rptProducCategory.DataBind();
    }

    public delegate void ProductCategorySelectEventHandler(object sender, ProductCategorySelectEventArgs e);
    public event ProductCategorySelectEventHandler Select;

    protected void OnSelect(object sender, ProductCategorySelectEventArgs e)
	{
		if (Select != null)
		{
			Select ( sender, e );
		}
	}
  
    public class ProductCategorySelectEventArgs : EventArgs
    {
        public int ProductCategoryId { get; set; }
        public string ParentString { get; set; }
        public string NodeString { get; set; }

        public ProductCategorySelectEventArgs(int productCategoryId, string parentString, string nodeString)
        {
            ProductCategoryId = productCategoryId;
            ParentString = parentString;
            NodeString = nodeString;
        }
    }

    protected void lnkProduct_Click(object sender, EventArgs e)
    {
        LinkButton lnk = (LinkButton) sender;
        int productCategoryId = Convert.ToInt32(lnk.CommandArgument);
        OnSelect(sender,new ProductCategorySelectEventArgs(productCategoryId,lnk.Text,lblCategory.Text));
    }
}
